'use strict';

const BaseController = require('./base');

class AddressController extends BaseController {
  constructor(ctx) {
    super(ctx, 'Address');
  }

  // 获取我的收货地址列表
  async getMyAddresses() {
    const { ctx } = this;
    const memberId = ctx.state.member.id;
    
    const list = await ctx.service.address.getMemberAddresses(memberId);
    ctx.success(list);
  }

  // 创建收货地址
  async create() {
    const { ctx } = this;
    const data = ctx.request.body;
    const memberId = ctx.state.member.id;
    
    data.memberId = memberId;
    const address = await ctx.service.address.createAddress(data);
    ctx.success(address);
  }

  // 更新收货地址
  async update() {
    const { ctx } = this;
    const id = ctx.params.id;
    const memberId = ctx.state.member.id;
    const data = ctx.request.body;
    
    const address = await ctx.service.address.updateAddress(id, memberId, data);
    ctx.success(address);
  }

  // 删除收货地址
  async destroy() {
    const { ctx } = this;
    const id = ctx.params.id;
    const memberId = ctx.state.member.id;
    
    await ctx.service.address.deleteAddress(id, memberId);
    ctx.success(id);
  }

  // 设置默认地址
  async setDefault() {
    const { ctx } = this;
    const id = ctx.params.id;
    const memberId = ctx.state.member.id;
    
    const address = await ctx.service.address.setDefaultAddress(id, memberId);
    ctx.success(address);
  }
}

module.exports = AddressController;
